<template>
  <div>
    <div class="goods_top_container">
      <a-button>添加会员</a-button>
      <div class="goods_search">
        <a-input v-model="val" placeholder="手机号/邮箱/会员昵称" />
        <a-button @click="onSearch">搜索</a-button>
      </div>
    </div>
    <a-table
      :columns="columns"
      :data-source="showlist"
      :rowKey="(record,index)=>{return index}"
      bordered
      :pagination="{
        defaultPageSize: 3,
        total: showlist.length,
      }"
    >
      <template slot="operation" slot-scope="text, record">
        <div class="goods_tab_button">
          <a-button type="primary" ghost @click="onClickDetails(record)">
            编辑
          </a-button>
          <a-button type="danger" ghost @click="onClickDel(record)">
            删除
          </a-button>
        </div>
      </template>
    </a-table>

    <div>
      <a-modal v-model="visible" title="修改" @ok="handleOk">
        <a-form-model ref="ruleForm" :model="message">
          <a-form-model-item has-feedback label="部门名称">
            <a-input v-model="message.name" type="text" autocomplete="off" />
          </a-form-model-item>
          <a-form-model-item has-feedback label="地址">
            <a-input v-model="message.dz" type="text" autocomplete="off" />
          </a-form-model-item>
        </a-form-model>
      </a-modal>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      columns: [
        {
          title: "Id",
          dataIndex: "id",
          key: "id",
        },
        {
          title: "部门昵称",
          dataIndex: "name",
          key: "name",
        },
        {
          title: "部门代码",
          dataIndex: "category",
          key: "category",
        },
        {
          title: "手机号",
          dataIndex: "phone",
          key: "phone",
        },
        {
          title: "地址",
          dataIndex: "address",
          key: "address",
        },
        {
          title: "操作",
          dataIndex: "operation",
          key: "operation",
          scopedSlots: { customRender: "operation" },
        },
      ],
      list: [], //保存一份数据用于搜索/分页
      showlist: [], //展示数据
      val: "", //输入搜索的关键字
      message: {
        name: "",
        dz: "",
        id: 0,
      },
      visible: false, //弹出框的初始状态
    };
  },
  mounted() {
    this.$axios.get("http://localhost:8080/shopList.json").then((res) => {
      this.list = res.data;
      this.showlist = res.data;
    });
  },
  methods: {
    onSearch() {
      //点击搜索
      if (this.val.length == 0) {
        return this.$message.error("搜索内容不能为空");
      }
      this.showlist = [];//清空显示数据
      this.list.map((el) => {
        if (el.name.includes(this.val) || el.category.includes(this.val)) {
          this.showlist.push(el);
        }
      });
    },
    //修改
    onClickDetails(record) {
      //显示弹出框
      this.visible = true;
      //定义一个message数据，让他们保持一致
      this.message.name = record.name;
      this.message.id = record.id;
      this.message.dz = record.address;
    },
    //修改成功OK
    handleOk() {
      //隐藏弹出框
      this.visible = false;

      //遍历数组，如果id相等，。和修改后的名字和地址保持一致
      this.showlist.map((el) => {
        if (el.id == this.message.id) {
          el.name = this.message.name;
          el.address = this.message.dz;
        }
      });
    },
    //删除
    onClickDel(record) {
    //   console.log(record);
      var index = this.showlist.findIndex((el) => {
        return el.id == record.id;
      });
      if (index != -1) {
        this.showlist.splice(index, 1);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.goods_top_container {
  width: 100%;
  height: 50px;
  display: inline-flex;
  justify-content: space-between;
  align-items: center;
  .goods_search {
    width: 32%;
    height: 100%;
    display: inline-flex;
    justify-content: space-between;
    align-items: center;
  }
}
.goods_tab_button {
  width: 100%;
  display: inline-flex;
  justify-content: space-between;
  align-items: center;
}
</style>